use :: libc;
extern "C" {
    fn grid_get_cell(_: *mut grid, _: u_int, _: u_int, _: *mut grid_cell);
    fn grid_get_line(_: *mut grid, _: u_int) -> *mut grid_line;
    fn grid_line_length(_: *mut grid, _: u_int) -> u_int;
    fn grid_in_set(_: *mut grid, _: u_int, _: u_int, _: *const i8) -> i32;
    fn memcmp(_: *const libc::c_void, _: *const libc::c_void, _: u64) -> i32;
}
pub type __u_char = u8;
pub type __u_short = u16;
pub type __u_int = u32;
pub type __time_t = i64;
pub type time_t = __time_t;
pub type u_char = __u_char;
pub type u_short = __u_short;
pub type u_int = __u_int;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_cell {
    pub data: utf8_data,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct utf8_data {
    pub data: [u_char; 21],
    pub have: u_char,
    pub size: u_char,
    pub width: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid {
    pub flags: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub hscrolled: u_int,
    pub hsize: u_int,
    pub hlimit: u_int,
    pub linedata: *mut grid_line,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_line {
    pub celldata: *mut grid_cell_entry,
    pub cellused: u_int,
    pub cellsize: u_int,
    pub extddata: *mut grid_extd_entry,
    pub extdsize: u_int,
    pub flags: i32,
    pub time: time_t,
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_extd_entry {
    pub data: utf8_char,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
pub type utf8_char = u_int;
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_cell_entry {
    pub c2rust_unnamed: C2RustUnnamed,
    pub flags: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed {
    pub offset: u_int,
    pub data: C2RustUnnamed_0,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub attr: u_char,
    pub fg: u_char,
    pub bg: u_char,
    pub data: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_reader {
    pub gd: *mut grid,
    pub cx: u_int,
    pub cy: u_int,
}
#[no_mangle]
pub extern "C" fn grid_reader_start(
    mut gr: *mut grid_reader,
    mut gd: *mut grid,
    mut cx: u_int,
    mut cy: u_int,
) {
    (unsafe { (*gr).gd = gd });
    (unsafe { (*gr).cx = cx });
    (unsafe { (*gr).cy = cy });
}
#[no_mangle]
pub extern "C" fn grid_reader_get_cursor(
    mut gr: *mut grid_reader,
    mut cx: *mut u_int,
    mut cy: *mut u_int,
) {
    (unsafe { *cx = (*gr).cx });
    (unsafe { *cy = (*gr).cy });
}
#[no_mangle]
pub extern "C" fn grid_reader_line_length(mut gr: *mut grid_reader) -> u_int {
    return unsafe { grid_line_length((*gr).gd, (*gr).cy) };
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_right(mut gr: *mut grid_reader, mut wrap: i32, mut all: i32) {
    let mut px: u_int = 0;
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    if all != 0 {
        px = unsafe { (*(*gr).gd).sx };
    } else {
        px = grid_reader_line_length(gr);
    }
    if wrap != 0
        && (unsafe { (*gr).cx }) >= px
        && (unsafe { (*gr).cy })
            < (unsafe { (*(*gr).gd).hsize })
                .wrapping_add(unsafe { (*(*gr).gd).sy })
                .wrapping_sub(1 as i32 as u32)
    {
        grid_reader_cursor_start_of_line(gr, 0 as i32);
        grid_reader_cursor_down(gr);
    } else if (unsafe { (*gr).cx }) < px {
        (unsafe { (*gr).cx = ((*gr).cx).wrapping_add(1) });
        while (unsafe { (*gr).cx }) < px {
            (unsafe { grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc) });
            if !(gc.flags as i32) & 0x4 as i32 != 0 {
                break;
            }
            (unsafe { (*gr).cx = ((*gr).cx).wrapping_add(1) });
        }
    }
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_left(mut gr: *mut grid_reader, mut wrap: i32) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    while (unsafe { (*gr).cx }) > 0 as i32 as u32 {
        (unsafe { grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc) });
        if !(gc.flags as i32) & 0x4 as i32 != 0 {
            break;
        }
        (unsafe { (*gr).cx = ((*gr).cx).wrapping_sub(1) });
    }
    if (unsafe { (*gr).cx }) == 0 as i32 as u32
        && (unsafe { (*gr).cy }) > 0 as i32 as u32
        && (wrap != 0
            || (unsafe {
                (*grid_get_line((*gr).gd, ((*gr).cy).wrapping_sub(1 as i32 as u32))).flags
            }) & 0x1 as i32
                != 0)
    {
        grid_reader_cursor_up(gr);
        grid_reader_cursor_end_of_line(gr, 0 as i32, 0 as i32);
    } else if (unsafe { (*gr).cx }) > 0 as i32 as u32 {
        (unsafe { (*gr).cx = ((*gr).cx).wrapping_sub(1) });
    }
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_down(mut gr: *mut grid_reader) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    if (unsafe { (*gr).cy })
        < (unsafe { (*(*gr).gd).hsize })
            .wrapping_add(unsafe { (*(*gr).gd).sy })
            .wrapping_sub(1 as i32 as u32)
    {
        (unsafe { (*gr).cy = ((*gr).cy).wrapping_add(1) });
    }
    while (unsafe { (*gr).cx }) > 0 as i32 as u32 {
        (unsafe { grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc) });
        if !(gc.flags as i32) & 0x4 as i32 != 0 {
            break;
        }
        (unsafe { (*gr).cx = ((*gr).cx).wrapping_sub(1) });
    }
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_up(mut gr: *mut grid_reader) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    if (unsafe { (*gr).cy }) > 0 as i32 as u32 {
        (unsafe { (*gr).cy = ((*gr).cy).wrapping_sub(1) });
    }
    while (unsafe { (*gr).cx }) > 0 as i32 as u32 {
        (unsafe { grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc) });
        if !(gc.flags as i32) & 0x4 as i32 != 0 {
            break;
        }
        (unsafe { (*gr).cx = ((*gr).cx).wrapping_sub(1) });
    }
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_start_of_line(mut gr: *mut grid_reader, mut wrap: i32) {
    if wrap != 0 {
        while (unsafe { (*gr).cy }) > 0 as i32 as u32
            && (unsafe {
                (*grid_get_line((*gr).gd, ((*gr).cy).wrapping_sub(1 as i32 as u32))).flags
            }) & 0x1 as i32
                != 0
        {
            (unsafe { (*gr).cy = ((*gr).cy).wrapping_sub(1) });
        }
    }
    (unsafe { (*gr).cx = 0 as i32 as u_int });
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_end_of_line(
    mut gr: *mut grid_reader,
    mut wrap: i32,
    mut all: i32,
) {
    let mut yy: u_int = 0;
    if wrap != 0 {
        yy = (unsafe { (*(*gr).gd).hsize })
            .wrapping_add(unsafe { (*(*gr).gd).sy })
            .wrapping_sub(1 as i32 as u32);
        while (unsafe { (*gr).cy }) < yy
            && (unsafe { (*grid_get_line((*gr).gd, (*gr).cy)).flags }) & 0x1 as i32 != 0
        {
            (unsafe { (*gr).cy = ((*gr).cy).wrapping_add(1) });
        }
    }
    if all != 0 {
        (unsafe { (*gr).cx = (*(*gr).gd).sx });
    } else {
        (unsafe { (*gr).cx = grid_reader_line_length(gr) });
    };
}
extern "C" fn grid_reader_handle_wrap(
    mut gr: *mut grid_reader,
    mut xx: *mut u_int,
    mut yy: *mut u_int,
) -> i32 {
    while (unsafe { (*gr).cx }) > (unsafe { *xx }) {
        if (unsafe { (*gr).cy }) == (unsafe { *yy }) {
            return 0 as i32;
        }
        grid_reader_cursor_start_of_line(gr, 0 as i32);
        grid_reader_cursor_down(gr);
        if (unsafe { (*grid_get_line((*gr).gd, (*gr).cy)).flags }) & 0x1 as i32 != 0 {
            (unsafe { *xx = ((*(*gr).gd).sx).wrapping_sub(1 as i32 as u32) });
        } else {
            (unsafe { *xx = grid_reader_line_length(gr) });
        }
    }
    return 1 as i32;
}
#[no_mangle]
pub extern "C" fn grid_reader_in_set(mut gr: *mut grid_reader, mut set: *const i8) -> i32 {
    return unsafe { grid_in_set((*gr).gd, (*gr).cx, (*gr).cy, set) };
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_next_word(
    mut gr: *mut grid_reader,
    mut separators: *const i8,
) {
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    let mut width: u_int = 0;
    if (unsafe { (*grid_get_line((*gr).gd, (*gr).cy)).flags }) & 0x1 as i32 != 0 {
        xx = (unsafe { (*(*gr).gd).sx }).wrapping_sub(1 as i32 as u32);
    } else {
        xx = grid_reader_line_length(gr);
    }
    yy = (unsafe { (*(*gr).gd).hsize })
        .wrapping_add(unsafe { (*(*gr).gd).sy })
        .wrapping_sub(1 as i32 as u32);
    if grid_reader_handle_wrap(gr, &mut xx, &mut yy) == 0 {
        return;
    }
    if grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) == 0 {
        if grid_reader_in_set(gr, separators) != 0 {
            loop {
                (unsafe { (*gr).cx = ((*gr).cx).wrapping_add(1) });
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && grid_reader_in_set(gr, separators) != 0
                    && grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) == 0)
                {
                    break;
                }
            }
        } else {
            loop {
                (unsafe { (*gr).cx = ((*gr).cx).wrapping_add(1) });
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && !(grid_reader_in_set(gr, separators) != 0
                        || grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) != 0))
                {
                    break;
                }
            }
        }
    }
    while grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0 && {
        width = grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) as u_int;
        width != 0
    } {
        (unsafe { (*gr).cx = ((*gr).cx as u32).wrapping_add(width) as u_int as u_int });
    }
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_next_word_end(
    mut gr: *mut grid_reader,
    mut separators: *const i8,
) {
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    if (unsafe { (*grid_get_line((*gr).gd, (*gr).cy)).flags }) & 0x1 as i32 != 0 {
        xx = (unsafe { (*(*gr).gd).sx }).wrapping_sub(1 as i32 as u32);
    } else {
        xx = grid_reader_line_length(gr);
    }
    yy = (unsafe { (*(*gr).gd).hsize })
        .wrapping_add(unsafe { (*(*gr).gd).sy })
        .wrapping_sub(1 as i32 as u32);
    while grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0 {
        if grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) != 0 {
            (unsafe { (*gr).cx = ((*gr).cx).wrapping_add(1) });
        } else if grid_reader_in_set(gr, separators) != 0 {
            loop {
                (unsafe { (*gr).cx = ((*gr).cx).wrapping_add(1) });
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && grid_reader_in_set(gr, separators) != 0
                    && grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) == 0)
                {
                    break;
                }
            }
            return;
        } else {
            loop {
                (unsafe { (*gr).cx = ((*gr).cx).wrapping_add(1) });
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && !(grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) != 0
                        || grid_reader_in_set(gr, separators) != 0))
                {
                    break;
                }
            }
            return;
        }
    }
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_previous_word(
    mut gr: *mut grid_reader,
    mut separators: *const i8,
    mut already: i32,
    mut stop_at_eol: i32,
) {
    let mut oldx: i32 = 0;
    let mut oldy: i32 = 0;
    let mut at_eol: i32 = 0;
    let mut word_is_letters: i32 = 0;
    if already != 0 || grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) != 0 {
        loop {
            if (unsafe { (*gr).cx }) > 0 as i32 as u32 {
                (unsafe { (*gr).cx = ((*gr).cx).wrapping_sub(1) });
                if grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) != 0 {
                    continue;
                }
                word_is_letters = (grid_reader_in_set(gr, separators) == 0) as i32;
                break;
            } else {
                if (unsafe { (*gr).cy }) == 0 as i32 as u32 {
                    return;
                }
                grid_reader_cursor_up(gr);
                grid_reader_cursor_end_of_line(gr, 0 as i32, 0 as i32);
                if !(stop_at_eol != 0 && (unsafe { (*gr).cx }) > 0 as i32 as u32) {
                    continue;
                }
                oldx = (unsafe { (*gr).cx }) as i32;
                (unsafe { (*gr).cx = ((*gr).cx).wrapping_sub(1) });
                at_eol = grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8);
                (unsafe { (*gr).cx = oldx as u_int });
                if at_eol == 0 {
                    continue;
                }
                word_is_letters = 0 as i32;
                break;
            }
        }
    } else {
        word_is_letters = (grid_reader_in_set(gr, separators) == 0) as i32;
    }
    loop {
        oldx = (unsafe { (*gr).cx }) as i32;
        oldy = (unsafe { (*gr).cy }) as i32;
        if (unsafe { (*gr).cx }) == 0 as i32 as u32 {
            if (unsafe { (*gr).cy }) == 0 as i32 as u32
                || !(unsafe {
                    (*grid_get_line((*gr).gd, ((*gr).cy).wrapping_sub(1 as i32 as u32))).flags
                }) & 0x1 as i32
                    != 0
            {
                break;
            }
            grid_reader_cursor_up(gr);
            grid_reader_cursor_end_of_line(gr, 0 as i32, 1 as i32);
        }
        if (unsafe { (*gr).cx }) > 0 as i32 as u32 {
            (unsafe { (*gr).cx = ((*gr).cx).wrapping_sub(1) });
        }
        if !(grid_reader_in_set(gr, b"\t \0" as *const u8 as *const i8) == 0
            && word_is_letters != grid_reader_in_set(gr, separators))
        {
            break;
        }
    }
    (unsafe { (*gr).cx = oldx as u_int });
    (unsafe { (*gr).cy = oldy as u_int });
}
extern "C" fn grid_reader_cell_equals_data(
    mut gc: *const grid_cell,
    mut ud: *const utf8_data,
) -> i32 {
    if (unsafe { (*gc).flags }) as i32 & 0x4 as i32 != 0 {
        return 0 as i32;
    }
    if (unsafe { (*gc).flags }) as i32 & 0x80 as i32 != 0
        && (unsafe { (*ud).size }) as i32 == 1 as i32
        && (unsafe { *((*ud).data).as_ptr() }) as i32 == '\t' as i32
    {
        return 1 as i32;
    }
    if (unsafe { (*gc).data.size }) as i32 != (unsafe { (*ud).size }) as i32 {
        return 0 as i32;
    }
    return ((unsafe {
        memcmp(
            ((*gc).data.data).as_ptr() as *const libc::c_void,
            ((*ud).data).as_ptr() as *const libc::c_void,
            (*gc).data.size as u64,
        )
    }) == 0 as i32) as i32;
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_jump(
    mut gr: *mut grid_reader,
    mut jc: *const utf8_data,
) -> i32 {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    px = unsafe { (*gr).cx };
    yy = (unsafe { (*(*gr).gd).hsize })
        .wrapping_add(unsafe { (*(*gr).gd).sy })
        .wrapping_sub(1 as i32 as u32);
    py = unsafe { (*gr).cy };
    while py <= yy {
        xx = unsafe { grid_line_length((*gr).gd, py) };
        while px < xx {
            (unsafe { grid_get_cell((*gr).gd, px, py, &mut gc) });
            if grid_reader_cell_equals_data(&mut gc, jc) != 0 {
                (unsafe { (*gr).cx = px });
                (unsafe { (*gr).cy = py });
                return 1 as i32;
            }
            px = px.wrapping_add(1);
        }
        if py == yy || (unsafe { (*grid_get_line((*gr).gd, py)).flags }) & 0x1 as i32 == 0 {
            return 0 as i32;
        }
        px = 0 as i32 as u_int;
        py = py.wrapping_add(1);
    }
    return 0 as i32;
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_jump_back(
    mut gr: *mut grid_reader,
    mut jc: *const utf8_data,
) -> i32 {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut xx: u_int = 0;
    xx = (unsafe { (*gr).cx }).wrapping_add(1 as i32 as u32);
    py = (unsafe { (*gr).cy }).wrapping_add(1 as i32 as u32);
    while py > 0 as i32 as u32 {
        px = xx;
        while px > 0 as i32 as u32 {
            (unsafe {
                grid_get_cell(
                    (*gr).gd,
                    px.wrapping_sub(1 as i32 as u32),
                    py.wrapping_sub(1 as i32 as u32),
                    &mut gc,
                )
            });
            if grid_reader_cell_equals_data(&mut gc, jc) != 0 {
                (unsafe { (*gr).cx = px.wrapping_sub(1 as i32 as u32) });
                (unsafe { (*gr).cy = py.wrapping_sub(1 as i32 as u32) });
                return 1 as i32;
            }
            px = px.wrapping_sub(1);
        }
        if py == 1 as i32 as u32
            || (unsafe { (*grid_get_line((*gr).gd, py.wrapping_sub(2 as i32 as u32))).flags })
                & 0x1 as i32
                == 0
        {
            return 0 as i32;
        }
        xx = unsafe { grid_line_length((*gr).gd, py.wrapping_sub(2 as i32 as u32)) };
        py = py.wrapping_sub(1);
    }
    return 0 as i32;
}
#[no_mangle]
pub extern "C" fn grid_reader_cursor_back_to_indentation(mut gr: *mut grid_reader) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    let mut oldx: u_int = 0;
    let mut oldy: u_int = 0;
    yy = (unsafe { (*(*gr).gd).hsize })
        .wrapping_add(unsafe { (*(*gr).gd).sy })
        .wrapping_sub(1 as i32 as u32);
    oldx = unsafe { (*gr).cx };
    oldy = unsafe { (*gr).cy };
    grid_reader_cursor_start_of_line(gr, 1 as i32);
    py = unsafe { (*gr).cy };
    while py <= yy {
        xx = unsafe { grid_line_length((*gr).gd, py) };
        px = 0 as i32 as u_int;
        while px < xx {
            (unsafe { grid_get_cell((*gr).gd, px, py, &mut gc) });
            if (gc.data.size as i32 != 1 as i32
                || (unsafe { *(gc.data.data).as_mut_ptr() }) as i32 != ' ' as i32)
                && !(gc.flags as i32) & 0x80 as i32 != 0
                && !(gc.flags as i32) & 0x4 as i32 != 0
            {
                (unsafe { (*gr).cx = px });
                (unsafe { (*gr).cy = py });
                return;
            }
            px = px.wrapping_add(1);
        }
        if !(unsafe { (*grid_get_line((*gr).gd, py)).flags }) & 0x1 as i32 != 0 {
            break;
        }
        py = py.wrapping_add(1);
    }
    (unsafe { (*gr).cx = oldx });
    (unsafe { (*gr).cy = oldy });
}
